﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System.Text.RegularExpressions;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class FileManagerPage : WatiNBase
    {
        #region Constructors

        public FileManagerPage(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }

        public FileManagerPage(IE ieInstance, string siteUrl, string dbName) : base(ieInstance, siteUrl, dbName) { }

        #endregion

        #region Public Properties

        #region FileUploads
        public FileUpload SelectFileDialog
        {
            get { return IEInstance.FileUpload(Find.ById(s => s.EndsWith("WebUpload_cmdBrowse"))); }
        }

        #endregion

        #region Spans
        public Span CurrentFolderSpan
        {
            get { return IEInstance.Span(Find.ByClass(s => s.Contains("TreeNodeSelected"))); }
        }
        public Span FoldersSpan
        {
            get { return IEInstance.Span(Find.ById(s => s.EndsWith("FileManager_pnlFolders"))); }
        }        
        public Span PortalRootSpan
        {
            get { return IEInstance.Span(Find.ByTitle("Portal Root")); }
        }
        public Span UploadMessage
        {
            get
            {
                return IEInstance.Span(Find.ById(s => s.EndsWith("lblMessage")));
            }
        }
        #endregion

        #region SelectLists
        public SelectList FolderTypeSelectList
        {
            get { return IEInstance.SelectList(Find.ById(s => s.EndsWith("FileManager_ddlStorageLocation"))); }
        }
        public SelectList UploadDestinationSelectList
        {
            get { return IEInstance.SelectList(Find.ById(s => s.EndsWith("WebUpload_ddlFolders"))); }
        }
        #endregion

        #region TextFields
        public TextField FolderNameTextField
        {
            get { return IEInstance.TextField(Find.ById(s => s.EndsWith("FileManager_txtNewFolder"))); }
        }
        public TextField FilterTextField
        {
            get { return IEInstance.TextField(Find.ById(s => s.EndsWith("FileManager_txtFilter"))); }
        }
        public TextField UserPermissionField
        {
            get { return IEInstance.TextField(Find.ById("FileManager_dgPermissions_txtUser")); }
        }
        #endregion

        #region Images and Image Collections
        public Image AddFolderButton
        {
            get { return IEInstance.Image(Find.ByTitle("Add Folder")); }
        }
        public Image DeleteFolderImage
        {
            get { return IEInstance.Image(Find.ByTitle("Delete Folder")); }
        }
        public Image MoveFileImage
        {
            get { return IEInstance.Image(Find.ByTitle("Move Files")); }
        }
        public Image RefreshImage
        {
            get { return IEInstance.Image(Find.ByTitle("Refresh")); }
        }
        public Image FolderExpandImage
        {
            get { return FoldersSpan.Image(Find.BySrc(s => s.Contains("/Max_12X12_Standard.png"))); }
        }
        public Image UploadImage
        {
            get { return IEInstance.Image(Find.ByTitle("Upload")); }
        }
        public Image CopyFilesImage
        {
            get { return IEInstance.Image(Find.ByTitle("Copy Files")); }
        }
        public Image DeleteFilesImage
        {
            get { return IEInstance.Image(Find.ByTitle("Delete Files")); }
        }
        public Image SynchronizeFilesImage
        {
            get { return IEInstance.Image(Find.ByTitle("Sychronize Files")); }
        }
        public Image FilterImage
        {
            get { return IEInstance.Image(Find.ByTitle("Filter")); }
        }
        public ImageCollection SecureDatabaseIcons
        {
            get { return FoldersSpan.Images.Filter(Find.BySrc(s => s.Contains("Sql_16X16_Standard"))); }
        }
        public ImageCollection SecureFolderIcons
        {
            get { return FoldersSpan.Images.Filter(Find.BySrc(s => s.Contains("SecurityRoles"))); }
        }
        
        #endregion

        #region Links
        public Link ReturnLink
        {
            get { return ContentPane.Link(Find.ByTitle("Return")); }
        }
        public Link UploadSelectedFileLink
        {
            get { return ContentPane.Link(Find.ByText("Upload File")); }
        }        
        public Link AddUserPermissionLink
        {
            get { return PermissionsGridDiv.Link(Find.ByText("Add")); }
        }
        public Link UpdatePermissionGridLink
        {
            get { return IEInstance.Link(Find.ByTitle("Update")); }
        }
        #endregion

        #region Tables
        public Table FileManagerTable
        {
            get { return IEInstance.Table(Find.ByClass("FileManager")); }
        }
        public Table FolderContentsTable
        {
            get { return ContentPane.Table(Find.ByClass("FileManager_FileList")); }
        }
        public Table FolderSecurityTable
        {
            get { return IEInstance.Div(Find.ById(s => s.EndsWith("FileManager_pnlSecurity"))).Table(Find.Any); }
        }
        #endregion

        #region CheckBoxes
        public CheckBox SelectFileCheckbox
        {
            get { return FolderContentsTable.TableRow(Find.ByClass(new Regex(@"FileManager_\w*Item"))).CheckBox(Find.ById(s => s.EndsWith("chkFile"))); }
        }
        public CheckBox DecompressZipFilesCheckbox
        {
            get { return IEInstance.CheckBox(Find.ById(s => s.EndsWith("WebUpload_chkUnzip"))); }
        }
        #endregion

        #region Buttons
        public Button MoveFileOKButton
        {
            get { return FileManagerTable.Button(Find.ById("btnMoveOK")); }
        }
        #endregion

        #region Divs
        public Div PermissionsGridDiv
        {
            get { return IEInstance.Div(Find.ByClass("dnnGrid dnnPermissionsGrid")); }
        }
        #endregion

        #endregion

        #region Public Methods

        public void SelectFile(string packagePath)
        {
            SelectFileDialog.Set(packagePath);
        }

        public void UploadFile(string FilePath)
        {
            UploadImage.ClickNoWait();
            SelectFile(FilePath);
            UploadSelectedFileLink.ClickNoWait();
        }
        public void CreateFolderInRoot(string folderType, string folderName)
        {
            PortalRootSpan.Click();
            FolderTypeSelectList.Select(folderType);
            FolderNameTextField.Value = folderName;
            AddFolderButton.Click();
        }
        public void CreateFolderInParentFolder(string folderParent, string folderType, string folderName)
        {
            Span parentSpan = IEInstance.Span(Find.ByTitle(folderParent));
            parentSpan.Click();
            FolderTypeSelectList.Select(folderType);
            FolderNameTextField.Value = folderName;
            AddFolderButton.Click();
        }
        public void ClickOpenFilesPermissionForRole(string roleName)
        {
            Image result = null;
            foreach (TableRow row in FolderSecurityTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(roleName))
                {
                    result = row.TableCells[1].Image(Find.Any);
                    break;
                }
                continue;
            }
            result.ClickNoWait();
        }
        public void ClickBrowseFilesPermissionForRole(string roleName)
        {
            CheckBox result = null;
            foreach (TableRow row in FolderSecurityTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(roleName))
                {
                    result = row.TableCells[2].CheckBox(Find.Any);
                    break;
                }
                continue;
            }
            result.Click();
        }
        public void ClickWriteFolderPermissionForRole(string roleName)
        {
            CheckBox result = null;
            foreach (TableRow row in FolderSecurityTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(roleName))
                {
                    result = row.TableCells[3].CheckBox(Find.Any);
                    break;
                }
                continue;
            }
            result.Click();
        }

        #endregion
    }
}